package com.yidiandao.business.data.model;

import com.yidiandao.business.constant.Common;
import com.yidiandao.business.constant.Urls;
import com.yidiandao.business.data.net.BaseResponse;
import com.yidiandao.business.data.net.NetBiz;
import com.yidiandao.business.data.net.NetListener;
import com.yidiandao.business.data.net.exception.BizErrorInfo;
import com.yidiandao.business.data.net.exception.NetErrorInfo;
import com.yidiandao.business.data.recyclertype.Article;
import com.yidiandao.business.data.recyclertype.ArticleComment;
import com.yidiandao.business.data.request.EnrollRequest;
import com.yidiandao.business.data.request.LikeArticleRequest;
import com.yidiandao.business.data.request.LikeCommentRequest;
import com.yidiandao.business.data.request.QueryArticleCommentsRequest;
import com.yidiandao.business.data.request.QueryArticleRequest;
import com.yidiandao.business.data.request.QueryEnrollPayStateRequest;
import com.yidiandao.business.data.request.SubmitArticleCommentRequest;
import com.yidiandao.business.data.response.EnrollResponse;
import com.yidiandao.business.data.response.QueryArticleCommentsResponse;
import com.yidiandao.business.data.response.QueryArticleResponse;
import com.yidiandao.business.data.response.QueryEnrollPayStateResponse;
import com.yidiandao.business.utils.PreferenceUtils;
import com.yidiandao.business.utils.UIUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 发现文章 Model
 * <p>
 * Created by ChenTao(chentao7v@gmail.com) on 2017/4/13 0013 15:37
 */
public class ArticleModel implements IArticleModel {

    private NetBiz mNetBiz = new NetBiz();

    private List<ArticleComment> mArticleComments = new ArrayList<>();

    private int articleCommentsPager;

    @Override
    public void loadComments(String pagerTag, final boolean isRefreshing, final String articleId, final OnLoadResultListener<List<ArticleComment>> listener) {
        if (isRefreshing) {
            articleCommentsPager = 1;
        }

        QueryArticleCommentsRequest request = new QueryArticleCommentsRequest();
        request.setUrl(Urls.DISCOVERY_COMMENTS);
        request.setTag(pagerTag);
        request.setArticleId(articleId);
        request.setToken(PreferenceUtils.getToken(UIUtils.getContext()));
        request.setPage(articleCommentsPager);
        request.setPageSize(Common.PAGER_SIZE);

        mNetBiz.sendGetRequest(request, QueryArticleCommentsResponse.class, new NetListener<QueryArticleCommentsResponse>() {
            @Override
            public void onSuccess(QueryArticleCommentsResponse response) {
                articleCommentsPager++;
                if (isRefreshing) {
                    mArticleComments.clear();
                }

                List<ArticleComment> commentList = response.getData().getCommentList();

                mArticleComments.addAll(commentList);

                if (listener != null) {
                    listener.onSuccess(mArticleComments, commentList.size() == 0, response.hasMoreData());
                }
            }

            @Override
            public boolean onError(NetErrorInfo netErrorInfo) {
                if (listener != null) {
                    listener.onFailed(netErrorInfo.getMessage());
                }
                return true;
            }

            @Override
            public boolean onFailed(BizErrorInfo bizErrorInfo) {
                if (listener != null) {
                    listener.onFailed(bizErrorInfo.getMessage());
                }
                return true;
            }

            @Override
            public void onFinal() {

            }
        });
    }

    @Override
    public void likeComment(String tag, String commentId, final OnLoadResultListener listener) {
        LikeCommentRequest request = new LikeCommentRequest();
        request.setToken(PreferenceUtils.getToken(UIUtils.getContext()));
        request.setUrl(Urls.LIKE_COMMENT);
        request.setCommentId(commentId);
        request.setTag(tag);

        mNetBiz.sendGetRequest(request, BaseResponse.class, new NetListener<BaseResponse>() {
            @Override
            public void onSuccess(BaseResponse baseResponse) {
                if (listener != null) {
                    listener.onSuccess(null, false, false);
                }
            }

            @Override
            public boolean onError(NetErrorInfo netErrorInfo) {
                if (listener != null) {
                    listener.onFailed(netErrorInfo.getMessage());
                }
                return true;
            }

            @Override
            public boolean onFailed(BizErrorInfo bizErrorInfo) {
                if (listener != null) {
                    listener.onFailed(bizErrorInfo.getMessage());
                }
                return true;
            }

            @Override
            public void onFinal() {

            }
        });
    }

    @Override
    public void submitComment(String pagerTag, String articleId, String text, final OnLoadResultListener listener) {
        SubmitArticleCommentRequest request = new SubmitArticleCommentRequest();
        request.setUrl(Urls.SUBMIT_ARTICLE_COMMENT);
        request.setToken(PreferenceUtils.getToken(UIUtils.getContext()));
        request.setTag(pagerTag);
        request.setArticleId(articleId);
        request.setContent(text);

        mNetBiz.sendPostRequest(request, BaseResponse.class, new NetListener<BaseResponse>() {
            @Override
            public void onSuccess(BaseResponse response) {
                if (listener != null) {
                    listener.onSuccess(null, false, false);
                }
            }

            @Override
            public boolean onError(NetErrorInfo netErrorInfo) {
                if (listener != null) {
                    listener.onFailed(netErrorInfo.getMessage());
                }
                return true;
            }

            @Override
            public boolean onFailed(BizErrorInfo bizErrorInfo) {
                if (listener != null) {
                    listener.onFailed(bizErrorInfo.getMessage());
                }
                return true;
            }

            @Override
            public void onFinal() {

            }
        });
    }

    @Override
    public void likeArticle(String tag, String articleId, final OnLoadResultListener listener) {

        LikeArticleRequest request = new LikeArticleRequest();
        request.setUrl(Urls.LIKE_ARTICLE);
        request.setTag(tag);
        request.setToken(PreferenceUtils.getToken(UIUtils.getContext()));
        request.setArticleId(articleId);

        mNetBiz.sendGetRequest(request, BaseResponse.class, new NetListener<BaseResponse>() {
            @Override
            public void onSuccess(BaseResponse response) {
                if (listener != null) {
                    listener.onSuccess(null, false, false);
                }
            }

            @Override
            public boolean onError(NetErrorInfo netErrorInfo) {
                if (listener != null) {
                    listener.onFailed(netErrorInfo.getMessage());
                }
                return true;
            }

            @Override
            public boolean onFailed(BizErrorInfo bizErrorInfo) {
                if (listener != null) {
                    listener.onFailed(bizErrorInfo.getMessage());
                }
                return true;
            }

            @Override
            public void onFinal() {

            }
        });
    }

    @Override
    public void getArticleInfo(String tag, String articleId, final OnLoadResultListener<Article> listener) {

        QueryArticleRequest request = new QueryArticleRequest();
        request.setUrl(Urls.GET_ARTICLE_INFO);
        request.setToken(PreferenceUtils.getToken(UIUtils.getContext()));
        request.setArticleId(articleId);
        request.setTag(tag);

        mNetBiz.sendGetRequest(request, QueryArticleResponse.class, new NetListener<QueryArticleResponse>() {
            @Override
            public void onSuccess(QueryArticleResponse response) {
                if (listener != null) {
                    listener.onSuccess(response.getData().getArticle(), false, false);
                }
            }

            @Override
            public boolean onError(NetErrorInfo netErrorInfo) {
                if (listener != null) {
                    listener.onFailed(netErrorInfo.getMessage());
                }
                return true;
            }

            @Override
            public boolean onFailed(BizErrorInfo bizErrorInfo) {
                if (listener != null) {
                    listener.onFailed(bizErrorInfo.getMessage());
                }
                return true;
            }

            @Override
            public void onFinal() {

            }
        });
    }

    @Override
    public void enroll(String tag, String enrollId, String userName, String phoneNumber, int personNumber, final OnLoadResultListener<EnrollResponse.Data> listener) {

        EnrollRequest request = new EnrollRequest();
        request.setTag(tag);
        request.setToken(PreferenceUtils.getToken(UIUtils.getContext()));
        request.setEnrollId(enrollId);
        request.setUserName(userName);
        request.setPhoneNumber(phoneNumber);
        request.setPersonNumber(personNumber + "");
        request.setUrl(Urls.ENROLL);

        mNetBiz.sendPostRequest(request, EnrollResponse.class, new NetListener<EnrollResponse>() {
            @Override
            public void onSuccess(EnrollResponse response) {
                if (listener != null) {
                    listener.onSuccess(response.getData(), false, false);
                }
            }

            @Override
            public boolean onError(NetErrorInfo netErrorInfo) {
                if (listener != null) {
                    listener.onFailed(netErrorInfo.getMessage());
                }
                return true;
            }

            @Override
            public boolean onFailed(BizErrorInfo bizErrorInfo) {
                if (listener != null) {
                    listener.onFailed(bizErrorInfo.getMessage());
                }
                return false;
            }

            @Override
            public void onFinal() {

            }
        });
    }

    @Override
    public void getEnrollPayState(String tag, String recId, final OnLoadResultListener<QueryEnrollPayStateResponse.Data> listener) {

        QueryEnrollPayStateRequest request = new QueryEnrollPayStateRequest();
        request.setToken(PreferenceUtils.getToken(UIUtils.getContext()));
        request.setTag(tag);
        request.setUrl(Urls.QUERY_ENROLL_PAY_STATE);
        request.setRecId(recId);

        mNetBiz.sendGetRequest(request, QueryEnrollPayStateResponse.class, new NetListener<QueryEnrollPayStateResponse>() {
            @Override
            public void onSuccess(QueryEnrollPayStateResponse response) {
                if (listener != null) {
                    listener.onSuccess(response.getData(), false, false);
                }
            }

            @Override
            public boolean onError(NetErrorInfo netErrorInfo) {
                return true;
            }

            @Override
            public boolean onFailed(BizErrorInfo bizErrorInfo) {
                if (listener != null) {
                    listener.onFailed(bizErrorInfo.getMessage());
                }
                return false;
            }

            @Override
            public void onFinal() {

            }
        });
    }
}
